﻿
//==============================判断浏览器版本====================
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
if (window.ActiveXObject) {
    Sys.ie = ua.match(/msie ([\d.]+)/)[1]
}

//以下进行测试
//if (Sys.ie && navigator.userAgent.indexOf("MSIE 6.0")>0) {
//    alert("您的浏览器版本过低！请升级浏览器！");
//    location.href = "http://windows.microsoft.com/zh-CN/internet-explorer/download-ie";
//}
//===========================公用方法===========================
function stringToJSON(obj) {
    try {
        return eval('(' + obj + ')');
        //return jQuery.parseJSON(obj);
    } catch (e) {
        alert("你的数据不符合规范！请检查！");
    }
}

String.prototype.getRequest = function () {
    var paraStr = window.location.search.slice(1);
    var paraList = paraStr.split(/\&/g);
    for (var i = 0; i < paraList.length; i++) {
        var pattern = new RegExp(this + "[?=\\=](.+)", "g");
        var mh = pattern.exec(paraList[i]);
        if (mh) {
            return decodeURIComponent(mh[1])
        }
    }
    return "";
}

String.prototype.GetValueByKey = function (key) {
    var paraList = paraStr.split(/\&/g);
    for (var i = 0; i < paraList.length; i++) {
        var pattern = new RegExp(key + "[?=\\=](.+)", "g");
        var mh = pattern.exec(paraList[i]);
        if (mh) {
            return decodeURIComponent(mh[1])
        }
    }
    return "";
}

var queryString = {
    __init__: function () {
        var paraList = window.location.search.slice(1).split(/\&/g);
        for (var i = 0; i < paraList.length; i++) {
            var pattern = /^(.+)[?=\\=](.+)/g,
              mp = pattern.exec(paraList[i]);
            if (mp) {
                this[mp[1]] = mp[2];
            }
        }
    }
};
queryString.__init__();

//$("textarea[maxlength]").keyup(function () {
//    var area = $(this);
//    var max = parseInt(area.attr("maxlength"), 10); //获取maxlength的值
//    if (max > 0) {
//        if (area.val().length > max) { //textarea的文本长度大于maxlength
//            area.val(area.val().substr(0, max)); //截断textarea的文本重新赋值
//        }
//    }
//});
function maxlengthx(obj) {
    var area = $(obj);
    var max = parseInt(area.attr("maxlength"), 10); //获取maxlength的值
    if (max > 0) {
        if (area.val().length > max) { //textarea的文本长度大于maxlength
            area.val(area.val().substr(0, max)); //截断textarea的文本重新赋值
        }
    }
}

//获取剩余字符
function getLeftWords(thisObj, showId, max) {
    var txt = $(thisObj).val();
    var left = max - parseInt(txt.length);
    $("#" + showId).html((left < 0 ? 0 : left));
}

function showDialog(url, name, width, height) {
    window.showModalDialog(url, window, "dialogWidth:" + width + "px;dialogHeight:" + height + "px");
}

///当前鼠标位置
function mouseCoords(ev) {
    if (ev.pageX || ev.pageY) {
        return {
            x: ev.pageX,
            y: ev.pageY
        };
    }
    return {
        x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y: ev.clientY + document.body.scrollTop - document.body.clientTop
    };
}


function GetCountPage(pageCount, pageSize) {
    if (pageCount < pageSize) {
        return 1;
    } else if (pageCount > 0) {
        if (pageCount % pageSize == 0) {
            return maxpage = pageCount / pageSize;
        } else {
            return parseInt((pageCount / pageSize)) + 1;
        }
    }
    return 0;
}

String.prototype.fill = function (data) {
    if (typeof data === "object" && data) {
        return this.replace(/{(.+?)}/g, function (e1, e2) {
            var res = data[e2];
            if (res == undefined || res == null) {
                return e1;
            } else {
                return res;
            }
        });
    }
};

String.prototype.format = function () {
    var args = arguments;
    return this.replace(/{(\d{1})}/g, function () {
        return args[arguments[1]];
    });
};

String.prototype.toArray = function () {
    var a = new Array();
    for (var i = 0; i < this.length; i++) {
        a[i] = this.charAt(i);
    }
    return a;
}

String.prototype.replaceAll = function (key, value) {
    return this.replace(new RegExp("{{0}}".format(key), "gm"), value);
}

String.prototype.replaceo = function (key, value) {
    return this.replace("{" + key + "}", value);
}

String.prototype.replaceAllF = function () {
    return this.replace(new RegExp('"', "gm"), "'");
}

String.prototype.replaceAllFk = function () {
    return this.replace(new RegExp('"', "gm"), '');
}

String.prototype.replacenbsp = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "&nbsp;");
}



String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim = function () {
    return this.replace(/(^\s*)/g, "");
}
String.prototype.rtrim = function () {
    return this.replace(/(\s*$)/g, "");
}

function MillisecondToDate(time) {
    if (time == null || time == "") {
        return "0 秒";
    }
    var newTime = (new Date()).getTime();
    var oldTime = (new Date(time.replace("-", "/"))).getTime();
    var msd = newTime - oldTime;
    var time = parseFloat(msd) / 1000;
    if (null != time && "" != time) {
        if (time > 60 && time < 60 * 60) {
            time = parseInt(time / 60.0) + "分钟";
        } else if (time >= 60 * 60 && time < 60 * 60 * 24) {
            time = parseInt(time / 3600.0) + "小时";
        } else if (time > 60 * 60 * 24) {
            time = parseInt(time / (3600.0 * 24)) + "天";
        } else {
            time = parseInt(time) + "秒";
        }
    } else {
        time = "0 秒";
    }
    return time;
}

(function ($) {
    $(function () {
        $.fn.extend({
            setErrormsg: function (msg) {
                this.next("b").remove();
                this.after("<b><div class=\"wrong_bj\"><i>" + msg + "</i><em></em></div></b>");
            },
            clearmsg: function () {
                this.next("b").remove();
            },
            setRight: function () {
                this.next("b").remove();
                this.after("<b><img src=\"/css/images/reg_gou.png\"/></b>");
            },
            regexpa: function (rg, msg) {
                $(this).clearmsg();
                var r = RegExp(rg);
                if (r.test($(this).val())) {
                    $(this).setRight();
                    return true;
                } else {
                    $(this).setErrormsg(msg);
                    return false;
                }
            }
        });
    });
})(jQuery);
$(document).ready(function () {
    if (!document.getElementById("basic-modal-content")) {
        $("body").append("<div id='basic-modal-content' style='display:none;' ></div>");
    }
});
var basic_modal_content;
(function ($) {
    $(function () {
        $.fn.extend({
            modal: function (options) {
                var ops = $.extend({ modal: true, resizable: false, width: "auto", clear: true }, options || {});
                if (ops.clear)
                {
                    ClosemsgModal();
                }
                $(this).dialog(ops);
                basic_modal_content = $(this);
            }
        });
    });
})(jQuery);

function ClosemsgModal() {
    $("#msgshow-modal-content").html("");
    $("#msgshow-modal-content").hide();
}

function setmsg(msg) {
    var temp = "<div class='success_box modal-header'><div class='modal-header'><p >{msg}</p></div></div>";
    showmsgmodalcontent(temp.replaceo("msg", msg));
    setTimeout("ClosemsgModal()", 1500);
}

function showinfo(msg) {
    var temp = '<div class="alert alert-info"><strong>提示！</strong>{msg}</div>';
    showmsghtml(temp.replaceo("msg", msg));
    setTimeout("ClosemsgModal()", 1500);
}

function setconfirm(msg, fn) {
    var temp = '<div style="width:250px" ><div class="modal-header"><h3 style="line-height:18px;font-size:20px;" >确认</h3></div><div class="modal-body">{msg}</div><div class="modal-footer">' +
'<input id="btnconfirmboxyes" type="button" class="btn btn-primary" value="是"><input id="btnconfirmboxno" type="button" class="btn" value="否">'+
'</div></div>';
    showmsgmodalcontent(temp.replaceo("msg", msg));
    $("#btnconfirmboxyes").die("click").live("click", function () {
        fn();
        closemodal();
    });
    $("#btnconfirmboxno").die("click").live("click", function () {
        closemodal();
    })
}

function seterror(msg) {
    var temp = "<div class='error_box modal-header'><div class='modal-header'><p >{msg}</p></div></div>";
    showmsgmodalcontent(temp.replaceo("msg", "<b style='color:red;'>" + msg + "</b>"));
    setTimeout("ClosemsgModal()", 1500);
}

function showerror(msg) {
    var temp = '<div class="alert alert-error"><strong>提示！</strong>{msg}</div>';
    showmsghtml(temp.replaceo("msg", "<b style='color:red;'>" + msg + "</b>"));
    setTimeout("ClosemsgModal()", 1500);
}

function setloading() {
    var temp = "<div class='success_box modal-header' style='width:170px;'><div class='modal-header'><img src='/css/images/loading.gif'/>数据加载中……</div></div>";
    showmsgmodalcontent(temp);
}

function showloading() {
    var temp = '<div class="alert fade in"> <img src="/css/images/loading.gif"/>数据加载中……</div>'
    showmsghtml(temp);
}

function setalert(msg) {
    var temp = "<div class='success_box modal-header'><div class='modal-header'><span style='font-size:14px; padding:15px 0;color:#4a4a4a;'>{msg}</span>" + "<input onclick='ClosemsgModal()' style='display: block;margin: 0 auto;' type='button' class='btn btn-primary' value='确定'></div></div>";
    showmsgmodalcontent(temp.replaceo("msg", msg));
}

function closemodal() {
    ClosemsgModal();
    if (basic_modal_content) {
        basic_modal_content.dialog("close");
        basic_modal_content.dialog("destroy");
    }
}

function showmsgmodalcontent(html) {
    if (!document.getElementById("msgshow-modal-content")) {
        $("body").append("<div id='msgshow-modal-content' ></div>");
    }
    if (!document.getElementById("styleshowmodal")) {
        $("#styleshowmsghtml").remove();
        $("body").append("<style id='styleshowmodal'>#msgshow-modal-content{z-index: 999;position:absolute;display:none;background:#fff;border-top:1px solid #999;border-bottom:1px solid #999;border-left:1px solid #999;border-right:1px solid #999;}</style>");
    }
    $("#msgshow-modal-content").html(html);
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#msgshow-modal-content").height();
    var popupWidth = $("#msgshow-modal-content").width();
    $("#msgshow-modal-content").css({
        "position": "absolute",
        "top": (windowHeight - popupHeight) / 2 + $(document).scrollTop(),
        "left": (windowWidth - popupWidth) / 2
    });
    $("#msgshow-modal-content").show();
}
function showmsghtml(html) {
    if (!document.getElementById("msgshow-modal-content")) {
        $("body").append("<div id='msgshow-modal-content' ></div>");
    }
    if (!document.getElementById("styleshowmsghtml"))
    {
        $("#styleshowmodal").remove();
        $("body").append("<style id='styleshowmsghtml' >#msgshow-modal-content{z-index: 999;position:absolute;display:none;}</style>");
    }
    $("#msgshow-modal-content").html(html);
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#msgshow-modal-content").height();
    var popupWidth = $("#msgshow-modal-content").width();
    var top= $(document).scrollTop();
    $("#msgshow-modal-content").css({
        "position": "absolute",
        "top":  top,
        "left": (windowWidth - popupWidth) / 2
    }).animate({ top: top +50}, 500);
    $("#msgshow-modal-content").show();
}

function strToInt(instr) {
    var nextstr = instr.toString();
    var obj = nextstr.toArray();
    if (parseInt(obj[0]) == 0) { //截取第一位等于0的
        nextstr = strToInt(instr.substring(1, instr.length));
    }
    return parseInt(nextstr);
}

function resNumToCH(str) {
    str = str.toString();
    var weis = ["", "十", "百", "千", "万", "十万", "百万", "千万", "亿"];
    var obj = str.toArray();
    var result = "";
    var chArray = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
    if (obj.length > 1) {
        var wei = obj.length;
        var swei = obj[0];
        result += chArray[parseInt(swei)] + weis[wei - 1];
        var nextstr = str.substring(1, str.length);
        var nextint = strToInt(nextstr);
        if (wei - (nextint.toString().length) > 1) {
            result += "零";
        }
        if (nextint > 0) {
            var r = resNumToCH(strToInt(nextstr));
            result += r;
        }
    } else {
        result += chArray[parseInt(obj[0])];
    }
    return result;
}

function NumToCH(str) {
    return resNumToCH(str).replace(/^一十/, "十");
}

function commentReplace(code) {
    if (code != "" && code != undefined) {
        var Url = "/css/face/gif/"
        code = code.replace(/\[@/g, "<img src=" + Url + "");
        code = code.replace(/\@]/g, ".gif />");
    }
    return code;
}

(function ($) {
    $(function () {
        $.fn.extend({
            ReplaceFace: function () {
                $(this).html(commentReplace($(this).html()));
            }
        });
    });
})(jQuery);

(function ($) {
    $(function () {
        $.fn.extend({
            ConverHtml: function () {
                $(this).html(ConverCode($(this).html()));
            }
        });
    });
})(jQuery);

function ConverCode(code) {
    if (code != "" && code != undefined) {
        code = code.replace(/\[url=(.+?)\](.+?)\[\/url\]/g, function (m, e1, e2) {
            return "<a href=" + e1 + ">" + e2 + "</a>";
        });
        code = code.replace(/\[i\](.+?)\[\/i\]/g, function (m, e1) {
            return "<i >" + e1 + "</i>";
        });
        code = code.replace(/\[b\](.+?)\[\/b\]/g, function (m, e1) {
            return "<b >" + e1 + "</b>";
        });
        code = code.replace(/\[u\](.+?)\[\/u\]/g, function (m, e1) {
            return "<u >" + e1 + "</u>";
        });
    }
    return code;
}



$(function () {
    $(document).ready(function () {
        $("ul.menu li").click(function () {
            $(this).addClass("hover").siblings().removeClass("hover");
        });
    });
});

copy2Clipboard = function (txt) {
    if (window.clipboardData) {
        window.clipboardData.clearData();
        window.clipboardData.setData("Text", txt);
    } else if (navigator.userAgent.indexOf("Opera") != -1) {
        window.location = txt;
    } else if (window.netscape) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        } catch (e) {
            alert("您的firefox安全限制限制您进行剪贴板操作，请打开'about:config'将signed.applets.codebase_principal_support'设置为true'之后重试，相对路径为firefox根目录/greprefs/all.js");
            return false;
        }
        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;
        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;
        trans.addDataFlavor('text/unicode');
        var str = new Object();
        var len = new Object();
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        var copytext = txt;
        str.data = copytext;
        trans.setTransferData("text/unicode", str, copytext.length * 2);
        var clipid = Components.interfaces.nsIClipboard;
        if (!clip) return false;
        clip.setData(trans, null, clipid.kGlobalClipboard);
    }
}
